package com.mcxx.modules.minimumliving.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.minimumliving.service.ExpireSalvationService;
import com.mcxx.modules.miniSalvation.family.dto.SalvationFamilyDTO;
import com.mcxx.modules.miniSalvation.family.param.ExpireFamilyParam;
import com.mcxx.modules.miniSalvation.family.param.SalvationFamilyParam;
import com.mcxx.modules.miniSalvation.order.dto.BatchExpireDTO;
import com.mcxx.modules.miniSalvation.order.dto.ExpireFamilyDTO;
import com.mcxx.modules.miniSalvation.order.dto.ExpireFamilyDetailDTO;
import com.mcxx.modules.miniSalvation.order.service.read.ExpireFamilyDetailReadService;
import com.mcxx.modules.miniSalvation.order.service.read.ExpireFamilyReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;

import javax.validation.Valid;


@Slf4j
@Validated
@Api("低保批量停保管理控制类")
@RestService({"minimumliving/salvation/batchstop/v2"})
public class BatchStopController extends DefaultRestService {

    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private ExpireSalvationService expireSalvationService;
    @Autowired
    private ExpireFamilyReadService expireFamilyReadService;
    @Autowired
    private ExpireFamilyDetailReadService expireFamilyDetailReadService;


    @PostMapping("getSalvationFamilyList")
    @ApiOperation("查询救助家庭列表")
    public void getSalvationFamilyList(SalvationFamilyParam param, PageParam pageParam) {
        log.info("开始查询救助家庭列表，参数是：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Page<SalvationFamilyDTO> page = orderReadService.getSalvationFamilyList4BatchStop(param, pageParam, accountVo);
        setPageBean(page);
        log.info("查询救助家庭列表完成，参数是：{}", JSON.toJSONString(page));
    }


    @PostMapping("stopSalvationBatch")
    @ApiOperation("发起批量停保")
    public void stopSalvationBatch(@RequestBody @Valid BatchExpireDTO batchExpireDTO) {
        log.info("开始进行批量停保，参数是：{}", JSON.toJSONString(batchExpireDTO));
        UserAccountVo accountVo = getCurUserAccount();
        batchExpireDTO.setBusinessType(Constant.BusinessCode.DIBAO);
        try {
            expireSalvationService.batchExpire(batchExpireDTO,accountVo,Constant.SuspendApproveType.SUSPEND_S);
            log.info("批量停保完成");
        } catch (BizException e) {
            setObjErrors(e.getErrCode(),e.getMessage());
            log.error(e.getMessage());
        }
    }


    @PostMapping("listExecutionPlan")
    @ApiOperation("查询批量停保执行计划")
    public void listExecutionPlan(ExpireFamilyParam param, PageParam pageParam) {
        log.info("开始查询批量停保执行计划，参数是：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Page<ExpireFamilyDTO> page = expireFamilyReadService.listExecutionPlan(param, pageParam, accountVo);
        setPageBean(page);
        log.info("查询批量停保执行计划完成，结果是：{}", JSON.toJSONString(page));
    }


    @PostMapping("listExecutionPlanDetail")
    @ApiOperation("查询批量停保执行计划明细（查询某一批停保家庭）")
    public void listExecPlanDetail(@V({"notnull"}) String expireId, PageParam pageParam) {
        log.info("开始查询批量停保执行计划明细（查询某一批停保家庭），参数是：{},{}", expireId,JSON.toJSONString(pageParam));
        Page<ExpireFamilyDetailDTO> page = expireFamilyDetailReadService.listExecutionPlanDetail(expireId,pageParam);
        setPageBean(page);
        log.info("查询批量停保执行计划明细（查询某一批停保家庭）完成，参数是：{}", JSON.toJSONString(page));
    }


    @PostMapping("undo")
    @ApiOperation("撤销停保")
    public void undo(@RequestPart @V({"notnull"}) String[] selectedRowKeys) {
        log.info("开始撤销停保，参数是：{}", JSON.toJSONString(selectedRowKeys));
        try {
            expireSalvationService.undo(selectedRowKeys);
            log.info("撤销停保完成");
        } catch (BizException e) {
            setObjErrors(e.getErrCode(),e.getMessage());
            log.error(e.getMessage());
        }
    }
}
